package com.lighthouse.base.content.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lighthouse.base.content.service.ContentService;
import com.lighthouse.base.content.vo.ContentVO;
import com.lighthouse.base.content.vo.SearchContentVO;


@Controller
public class ContentController {
	
	private static final Logger logger = LoggerFactory.getLogger(ContentController.class);
	
    @Resource (name="ContentService")
    private ContentService contentService;
    
	@RequestMapping(value = "/contentList")
	public String contentList(Locale locale, Model modelMap, @ModelAttribute SearchContentVO searchContentVo, HttpSession sess) {
		
		// Get Date, time
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		
		// 데이터 수집 & 페이지 지정
		int nTotalCount = contentService.getCount(searchContentVo);
		searchContentVo.setTotalCount(nTotalCount);
		//searchContentVo.setFirstIndex(0);
		searchContentVo.setRowCountPerPage(10);
		searchContentVo.setShowPageCount(3);
		
		List<ContentVO> contentVoList = null;
		contentVoList = contentService.getContentList(searchContentVo);
		if(logger.isDebugEnabled()) logger.debug("baseInfo Test is OK!!!");
		
		//jstl용 데이터 저장
		sess.setAttribute("skin", "skin-default");
		modelMap.addAttribute("serverTime", formattedDate );
		modelMap.addAttribute("TotalCount", nTotalCount );
		modelMap.addAttribute("searchContent", searchContentVo );
		modelMap.addAttribute("ContentList", contentVoList);
		
		return "content/contentlist";
	}
	
	@RequestMapping(value = "/content/getContentInfo")
	public String getContentInfo(Locale locale, Model modelMap, @ModelAttribute SearchContentVO searchContentVo, HttpSession sess) {
				
		ContentVO contentVo = contentService.getContentInfo(searchContentVo);
	
		//jstl용 데이터 저장
		sess.setAttribute("skin", "skin-default");
		modelMap.addAttribute("Content", contentVo);
		
		return "content/detail";
	}	
}
